package org.webdb.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.healthmarketscience.sqlbuilder.SqlObject;

public class SequenceStrategy implements ValueStrategy {

    private String sequenceName;

    public SequenceStrategy(String sequenceName) {
        this.sequenceName = sequenceName;
    }

    public Object generateValue(JDBCSqlProvider sqlProvider,
            JDBCProvider jdbcProvider) throws SQLException {
        SqlObject callNextSequenceValue = sqlProvider
                .callSequenceNextValue(sequenceName);
        ResultSet rs = jdbcProvider.executeQuery(callNextSequenceValue);
        rs.next();
        try {
            return rs.getObject(1);
        } finally {
            JDBCUtils.close(rs);
        }
    }
}
